<?php

/**
 * Tagger Plugin for Frog CMS <http://thehub.silentworks.co.uk/plugins/frog-cms/tagger>
 * Alternate Mirror site <http://www.tbeckett.net/articles/plugins/tagger.xhtml>
 * Copyright (C) 2008 Andrew Smith <a.smith@silentworks.co.uk>
 * Copyright (C) 2008 Tyler Beckett <tyler@tbeckett.net>

 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.

 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Include my Tagger Model
include_once 'models/Tagger.php';
include_once 'models/TaggerTag.php';

class TaggerController extends PluginController
{
    public function __construct()
    {
        $this->setLayout('backend');
        $this->assignToLayout('sidebar', new View('../../../plugins/tagger/views/sidebar'));
    }

    public function index()
    {
		function cmpVals2($val1, $val2) 
		{
			return strcasecmp($val1->name, $val2->name);
		};
		
		$tags = Record::findAllFrom('Tag', '1=1');
		usort($tags, 'cmpVals2');
		
        $this->display('tagger/views/index', array(
            'tags' => $tags
        ));
    }

    public function add()
    {
        // check if trying to save
        if (get_request_method() == 'POST')
            return $this->_add();

        // check if user have already enter something
        $tag = Flash::get('post_data');

        if (empty($tag))
            $tag = new Tagger;

        $this->display('tagger/views/edit', array(
            'action'  => 'add',
            'tag' => $tag
        ));
    }

    public function _add()
    {
        $data = $_POST['tag'];
        Flash::set('post_data', (object) $data);

        $tag = new Tag($data);

        if ( ! $tag->save())
        {
            Flash::set('error', __('Tag has not been added. Name must be unique!'));
            redirect(get_url('plugin/tagger', 'add'));
        }
        else Flash::set('success', __('Tag has been added!'));

        // save and quit or save and continue editing?
        if (isset($_POST['commit']))
            redirect(get_url('plugin/tagger'));
        else
            redirect(get_url('plugin/tagger/edit/'.$tag->id));
    }

    function edit($id)
    {
        if ( ! $tag = Tagger::findById($id))
        {
            Flash::set('error', __('Tag not found!'));
            redirect(get_url('plugin/tagger'));
        }

        // check if trying to save
        if (get_request_method() == 'POST')
            return $this->_edit($id);

        $this->display('tagger/views/edit', array(
            'action'  => 'edit',
            'tag' => $tag
        ));
    }

    function _edit($id)
    {
        $data = $_POST['tag'];

        $data['id'] = $id;

        $tag = new Tagger($data);

        if ( ! $tag->save())
        {
            Flash::set('error', __('Tag :name has not been saved. Name must be unique!', array(':name'=>$tag->name)));
            redirect(get_url('plugin/tagger/edit/'.$id));
        }
        else Flash::set('success', __('Tag :name has been saved!', array(':name'=>$tag->name)));

        // save and quit or save and continue editing?
        if (isset($_POST['commit']))
            redirect(get_url('plugin/tagger'));
        else
            redirect(get_url('plugin/tagger/edit/'.$id));
    }

    function delete($id)
    {
        // find the user to delete
        if ($tag = Record::findByIdFrom('Tag', $id))
        {
            if ($tag->delete()){
                if(TaggerTag::deleteByTagId($id))
                    Flash::set('success', __('Tag :name has been deleted!', array(':name'=>$tag->name)));
            }
            else
                Flash::set('error', __('Tag :name has not been deleted!', array(':name'=>$tag->name)));
        }
        else Flash::set('error', __('Tag not found!'));

        redirect(get_url('plugin/tagger'));
    }
	
	public function documentation()
    {
        $this->display('tagger/views/documentation');
    }
} // end FileController class
